package com.chianghao.qyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chianghao.qyt.entity.SysOperationAdminRole;
import com.chianghao.qyt.entity.SysOperationMenu;
import com.chianghao.qyt.entity.SysOperationRoleMenu;
import com.chianghao.qyt.mapper.SysOperationAdminRoleMapper;
import com.chianghao.qyt.mapper.SysOperationMenuMapper;
import com.chianghao.qyt.mapper.SysOperationRoleMenuMapper;
import com.chianghao.qyt.service.SysOperationMenuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysOperationMenuServiceImpl implements SysOperationMenuService {

    @Resource
    SysOperationMenuMapper sysOperationMenuMapper;

    @Resource
    SysOperationAdminRoleMapper sysOperationAdminRoleMapper;

    @Resource
    SysOperationRoleMenuMapper sysOperationRoleMenuMapper;


    @Override
    public List<SysOperationMenu> allMenus() {
        return sysOperationMenuMapper.selectList(null);
    }

    @Override
    public List<SysOperationMenu> loadUserMenus(Long userId) {
        List<SysOperationAdminRole> roles = sysOperationAdminRoleMapper.selectList(
                new QueryWrapper<SysOperationAdminRole>().lambda()
                        .eq(SysOperationAdminRole::getAdminId,userId)
        );
        if(roles ==null || roles.isEmpty()){
            return null;
        }

        List<SysOperationRoleMenu> roleMenus =sysOperationRoleMenuMapper.selectList(
                new QueryWrapper<SysOperationRoleMenu>().lambda()
                        .in(SysOperationRoleMenu::getRoleId,roles.stream()
                                .map(SysOperationAdminRole::getRoleId).collect(Collectors.toSet()))
        );
        if(roleMenus ==null || roleMenus.isEmpty()){
            return null;
        }

        return this.sysOperationMenuMapper.selectList(new QueryWrapper<SysOperationMenu>().lambda()
                .in(SysOperationMenu::getId,roleMenus.stream()
                        .map(SysOperationRoleMenu::getMenuId).collect(Collectors.toSet()))
        );
    }
}
